package xyz.zeer.endangeredanimal;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ThreatAdapter extends RecyclerView.Adapter<ThreatAdapter.ViewHolder> {

   private static List<ThreatModel> yrs_threatsModelList;
   private static Context yrs_context;
   private static SelectedImage yrs_selectedImage;

    public ThreatAdapter(List<ThreatModel> threatsModelList, Context context, SelectedImage selectedImage){
        this.yrs_threatsModelList = threatsModelList;
        this.yrs_context = context;
        this.yrs_selectedImage = selectedImage;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.yrs_threat_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int image = yrs_threatsModelList.get(position).getAnimalImageResource();
        String yrs_threat = yrs_threatsModelList.get(position).getFact();
        String name = yrs_threatsModelList.get(position).getAnimalName();

        ViewHolder.setData(image, yrs_threat, name);
    }

    @Override
    public int getItemCount() {
        return yrs_threatsModelList.size();
    }

   public interface SelectedImage{
        void selectedImage(ThreatModel yrs_threatsModel, Context context);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private static ImageView imageView;
        private static TextView textView;
        private static TextView textTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageAnimal);
            textView = itemView.findViewById(R.id.textVewThreat);
            textTitle = itemView.findViewById(R.id.Name);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yrs_selectedImage.selectedImage(yrs_threatsModelList.get(getAdapterPosition()), yrs_context);
                }
            });

        }

        public static void setData(int image, String yrs_threat, String name) {
            imageView.setImageResource(image);
            textView.setText(yrs_threat);
            textTitle.setText(name);
        }
    }
}
